
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>存储 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Landen">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-image-captions/image-captions.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="事件.html" />
    
    
    <link rel="prev" href="对象.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../01html/html简介.html">
            
                <a href="../01html/html简介.html">
            
                    
                    HTML
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../01html/01html.html">
            
                <a href="../01html/01html.html">
            
                    
                    a链接、图片
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../01html/02html.html">
            
                <a href="../01html/02html.html">
            
                    
                    列表、表格、表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../01html/h5新增.html">
            
                <a href="../01html/h5新增.html">
            
                    
                    h5新增
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../02css/css简介.html">
            
                <a href="../02css/css简介.html">
            
                    
                    CSS
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../02css/居中、省略、清浮.html">
            
                <a href="../02css/居中、省略、清浮.html">
            
                    
                    居中、省略、清浮
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../02css/浮动和定位.html">
            
                <a href="../02css/浮动和定位.html">
            
                    
                    浮动和定位
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../02css/变换、过渡、动画.html">
            
                <a href="../02css/变换、过渡、动画.html">
            
                    
                    变换、过渡、动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../02css/sass和rem.html">
            
                <a href="../02css/sass和rem.html">
            
                    
                    sass和rem
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../02css/flex、media、栅格化.html">
            
                <a href="../02css/flex、media、栅格化.html">
            
                    
                    flex、media、栅格化
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="js简介.html">
            
                <a href="js简介.html">
            
                    
                    JavaScript
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="数据类型.html">
            
                <a href="数据类型.html">
            
                    
                    数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="数组.html">
            
                <a href="数组.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="回流和重绘.html">
            
                <a href="回流和重绘.html">
            
                    
                    回流和重绘
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="浏览器的渲染过程.html">
            
                <a href="浏览器的渲染过程.html">
            
                    
                    浏览器的渲染过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="十大排序算法.html">
            
                <a href="十大排序算法.html">
            
                    
                    十大排序算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="对象的构建.html">
            
                <a href="对象的构建.html">
            
                    
                    对象的构建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="this.html">
            
                <a href="this.html">
            
                    
                    this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="bind&call&apply.html">
            
                <a href="bind&call&apply.html">
            
                    
                    bind&call&apply
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.11" data-path="变量提升.html">
            
                <a href="变量提升.html">
            
                    
                    变量提升
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.12" data-path="作用域.html">
            
                <a href="作用域.html">
            
                    
                    作用域
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.13" data-path="对象.html">
            
                <a href="对象.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.14" data-path="存储.html">
            
                <a href="存储.html">
            
                    
                    存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.15" data-path="事件.html">
            
                <a href="事件.html">
            
                    
                    事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.16" data-path="图片预览.html">
            
                <a href="图片预览.html">
            
                    
                    图片预览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.17" data-path="防抖节流.html">
            
                <a href="防抖节流.html">
            
                    
                    防抖节流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.18" data-path="同步异步.html">
            
                <a href="同步异步.html">
            
                    
                    同步异步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.19" data-path="babel入门.html">
            
                <a href="babel入门.html">
            
                    
                    babel入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.20" data-path="nvm.html">
            
                <a href="nvm.html">
            
                    
                    nvm
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.21" data-path="ES6.html">
            
                <a href="ES6.html">
            
                    
                    ES6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.22" data-path="ES6-ES11.html">
            
                <a href="ES6-ES11.html">
            
                    
                    ES6-ES11
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >存储</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x51E0;&#x79CD;&#x5B58;&#x50A8;&#x65B9;&#x5F0F;">&#x51E0;&#x79CD;&#x5B58;&#x50A8;&#x65B9;&#x5F0F;</h2>
<ol>
<li>sessionStorage</li>
<li>localStorage</li>
<li>cookie</li>
<li><p>session</p>
</li>
<li><p>&#x4ECE;&#x5B58;&#x50A8;&#x5927;&#x5C0F;&#x6765;&#x533A;&#x5206;&#xFF0C;cookie&#x6700;&#x5C0F;&#xFF0C;sessionStorage,&#x548C;localStorage&#x8F83;&#x5927;&#xFF0C;&#x5927;&#x7EA6;&#x5728;5M&#x5DE6;&#x53F3;&#x3002;</p>
</li>
<li>&#x4ECE;&#x65F6;&#x6548;&#x6027;&#x6765;&#x533A;&#x5206;&#xFF0C;localStorage&#x662F;&#x6C38;&#x4E45;&#x5B58;&#x50A8;&#xFF0C;&#x9664;&#x975E;&#x4E3B;&#x52A8;&#x5220;&#x9664;&#xFF0C;sessionStorage&#x5728;&#x9875;&#x9762;&#x5173;&#x95ED;&#x7684;&#x65F6;&#x5019;&#x6D88;&#x5931;&#xFF0C;cookie&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x5931;&#x6548;&#x65F6;&#x95F4;&#x3002;</li>
<li>&#x4ECE;&#x4F7F;&#x7528;&#x573A;&#x666F;&#x6765;&#x533A;&#x5206;&#xFF0C;localStorage &#x53EF;&#x4EE5;&#x7528;&#x6765;&#x5B58;&#x50A8;&#x6BD4;&#x8F83;&#x91CD;&#x8981;&#xFF0C;&#x7ECF;&#x5E38;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4E0D;&#x4F1A;&#x4E22;&#x5931;&#x3002;sessionStorage &#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5728;&#x767B;&#x5F55;&#x4F1A;&#x8BDD;&#x754C;&#x9762; cookie &#x591A;&#x4F7F;&#x7528;&#x5728;&#x4E0E;&#x670D;&#x52A1;&#x7AEF;&#x901A;&#x4FE1;&#x7684;&#x65F6;&#x5019;&#x7684;&#x6570;&#x636E;&#x3002;</li>
<li>cookie&#x6CA1;&#x6709;&#x66F4;&#x52A0;&#x5177;&#x4F53;&#x7684;api&#x64CD;&#x4F5C;&#xFF0C;&#x9700;&#x8981;&#x81EA;&#x5DF1;&#x5C01;&#x88C5;&#x4E00;&#x7CFB;&#x5217;&#x589E;&#x5220;&#x6539;&#x67E5;&#x3002;</li>
<li>session&#x7528;&#x4E8E;&#x670D;&#x52A1;&#x7AEF;&#x5B58;&#x50A8;&#x3002;&#x548C;cookie&#x7C7B;&#x4F3C;&#x3002;</li>
</ol>
<pre><code class="lang-js"><span class="hljs-comment">// &#x4F7F;&#x7528;&#x65B9;&#x6CD5;</span>
<span class="hljs-comment">// &#x5B58;&#x50A8;&#xFF1A;</span>
localStorage.setItem(name,value);
sessionStorage.setItem(name,value);
<span class="hljs-built_in">document</span>.cookie=<span class="hljs-string">&apos;name=value&apos;</span>;<span class="hljs-comment">//&#x4E00;&#x822C;&#x4F1A;&#x4F7F;&#x7528;&#x5C01;&#x88C5;&#x597D;&#x7684;&#x65B9;&#x6CD5;</span>
<span class="hljs-comment">// &#x83B7;&#x53D6;&#xFF1A;</span>
localStorage.getItem(name,value);
sessionStorage.getItem(name,value);
<span class="hljs-built_in">document</span>.cookie=<span class="hljs-string">&apos;name&apos;</span>;<span class="hljs-comment">//&#x4E00;&#x822C;&#x4F1A;&#x4F7F;&#x7528;&#x5C01;&#x88C5;&#x597D;&#x7684;&#x65B9;&#x6CD5;</span>

<span class="hljs-comment">// cookie&#x7684;&#x65B9;&#x6CD5;&#x5C01;&#x88C5;&#xFF1A;</span>
<span class="hljs-comment">// &#x8BBE;&#x7F6E;cookie</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setCookie</span>(<span class="hljs-params">name, value, times</span>) </span>{
    <span class="hljs-keyword">let</span> t=<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>();
    t.setDate(times);
    t.toUTCString();
    <span class="hljs-built_in">document</span>.cookie = <span class="hljs-string">`<span class="hljs-subst">${name}</span>=<span class="hljs-subst">${value}</span>;expires=<span class="hljs-subst">${times}</span>`</span>
}
<span class="hljs-comment">// &#x83B7;&#x53D6;cookie</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getCookie</span>(<span class="hljs-params">name</span>)</span>{
    <span class="hljs-keyword">let</span> value;
    <span class="hljs-keyword">let</span> cookie=<span class="hljs-built_in">document</span>.cookie;
    <span class="hljs-keyword">let</span> arr=cookie.split(<span class="hljs-string">&apos;;&apos;</span>)
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;arr.length;i++){
        <span class="hljs-keyword">let</span> s=arr[i].indexOf(<span class="hljs-string">&apos;=&apos;</span>);
        <span class="hljs-keyword">if</span>(s&gt;<span class="hljs-number">-1</span>){
            <span class="hljs-built_in">console</span>.info(arr[i].substring(<span class="hljs-number">0</span>,s))
            <span class="hljs-keyword">if</span>((arr[i].substring(<span class="hljs-number">0</span>,s)).replace(<span class="hljs-regexp">/\s/</span>,<span class="hljs-string">&apos;&apos;</span>)==name){
                <span class="hljs-keyword">let</span> a=arr[i].indexOf(<span class="hljs-string">&apos;=&apos;</span>);
                value=arr[i].substring(a+<span class="hljs-number">1</span>,arr[i].length)
            }
        }

    }
    <span class="hljs-keyword">return</span> value;
}
<span class="hljs-comment">// &#x5220;&#x9664;cookie,&#x53EF;&#x4EE5;&#x628A;&#x8FC7;&#x671F;&#x65F6;&#x95F4;&#x8BBE;&#x7F6E;&#x6210;&#x8D1F;&#x6570;&#x3002;</span>
</code></pre>

                                
                                </section>
                            
                        </div>
                    </div>
                
            </div>

            
                
                <a href="对象.html" class="navigation navigation-prev " aria-label="Previous page: 对象">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="事件.html" class="navigation navigation-next " aria-label="Next page: 事件">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"存储","level":"1.4.14","depth":2,"next":{"title":"事件","level":"1.4.15","depth":2,"path":"md/03js/事件.md","ref":"./md/03js/事件.md","articles":[]},"previous":{"title":"对象","level":"1.4.13","depth":2,"path":"md/03js/对象.md","ref":"./md/03js/对象.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-search","-sharing","toggle-chapters","image-captions","-anchor-navigation","-anchor-navigation-ex"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"image-captions":{"attributes":{"style":"left: 50%;position: relative;transform: translateX(-50%);"},"caption":"图 _PAGE_LEVEL_._PAGE_IMAGE_NUMBER_ - _CAPTION_","align":"center","variable_name":"_pictures"},"fontsettings":{"theme":"night","family":"sans","size":2},"toggle-chapters":{},"highlight":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Landen","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{"_pictures":[]},"gitbook":"3.x.x"},"file":{"path":"md/03js/存储.md","mtime":"2020-07-29T13:32:47.079Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-08-17T10:41:18.504Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

